﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using EntitiesTM;
using System.Web.UI;

namespace TimeWork
{
    public partial class ListStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] == null)
            {
                Response.Redirect("http://" + Request.Url.Authority + "/Login.aspx");
            }
            if (!IsPostBack)
            {
                FillGrid();
            }
            
        }
        public void FillGrid()
        {
            GVListStaff.DataSource = BuStaff.selAllStaff(Session["companyid"].ToString());
            GVListStaff.DataBind();
        }

        protected void GVListStaff_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataTable resultUnit = BuResultUnit.selAllResultUnit(Session["companyid"].ToString());
            DataTable permission = BuPermission.selAllPermission(Session["companyid"].ToString());
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink hplStaffEmail = (HyperLink)e.Row.FindControl("hplStaffEmail");
                if (hplStaffEmail != null)
                {
                    hplStaffEmail.NavigateUrl = "~/user/EditStaff.aspx?email=" + GVListStaff.DataKeys[e.Row.RowIndex].Values[0].ToString();
                }
                DropDownList ddlResultUnitName = (DropDownList)e.Row.FindControl("ddlResultUnitName");
                if (ddlResultUnitName != null)
                {
                    ddlResultUnitName.DataSource = resultUnit;
                    ddlResultUnitName.DataTextField = "ResultUnitName";
                    ddlResultUnitName.DataValueField = "ResultUnitID";
                    ddlResultUnitName.DataBind();
                    ddlResultUnitName.SelectedValue = GVListStaff.DataKeys[e.Row.RowIndex].Values[1].ToString();
                }

                DropDownList ddlPermissionName = (DropDownList)e.Row.FindControl("ddlPermissionName");
                if (ddlPermissionName != null)
                {
                    ddlPermissionName.DataSource = permission;
                    ddlPermissionName.DataTextField = "PermissionName";
                    ddlPermissionName.DataValueField = "PermissionID";
                    ddlPermissionName.DataBind();
                    ddlPermissionName.SelectedValue = GVListStaff.DataKeys[e.Row.RowIndex].Values[2].ToString();
                }
            }
        }

        protected void GVListStaff_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GVListStaff.EditIndex = e.NewEditIndex;
            FillGrid();
        }

        protected void GVListStaff_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GVListStaff.EditIndex = -1;
            FillGrid();
        }

        protected void GVListStaff_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            EnStaff staff = new EnStaff();
            staff.staffEmail = ((HyperLink) GVListStaff.Rows[e.RowIndex].FindControl("hplStaffEmail")).Text;
            staff.staffPosition = ((TextBox) GVListStaff.Rows[e.RowIndex].FindControl("txtStaffPosition")).Text;
            staff.staffName = ((TextBox) GVListStaff.Rows[e.RowIndex].FindControl("txtStaffName")).Text;
            DropDownList ddl = (DropDownList)GVListStaff.Rows[e.RowIndex].FindControl("ddlResultUnitName");
            string tmp = ddl.SelectedValue;

            string value = ((DropDownList)GVListStaff.Rows[e.RowIndex].Cells[3].Controls[1]).SelectedValue;
            staff.staffResultUnitID = Int32.Parse(((DropDownList)GVListStaff.Rows[e.RowIndex].FindControl("ddlResultUnitName")).SelectedValue);
            staff.staffPermissionID = Int32.Parse(((DropDownList)GVListStaff.Rows[e.RowIndex].FindControl("ddlPermissionName")).SelectedValue);
            BuStaff.updStaffOpt(staff, Session["companyid"].ToString());

            GVListStaff.EditIndex = -1;
            FillGrid();
        }

        //public void FillDropDownList()
        //{
        //    ddlDepartment.DataSource = BuDepartment.selAllDepartment();
        //    ddlDepartment.DataTextField = "DepartmentName";
        //    ddlDepartment.DataValueField = "DepartmentID";
        //    ddlDepartment.DataBind();

        //    ddlPermission.DataSource = BuPermission.selAllPermission();
        //    ddlPermission.DataTextField = "PermissionName";
        //    ddlPermission.DataValueField = "PermissionID";
        //    ddlPermission.DataBind();
        //}

        //protected void btnAddExpress_Click(object sender, EventArgs e)
        //{
        //    lblErrorEmail.Text = String.Empty;
        //    lblErrorName.Text = String.Empty;
        //    lblErrorPassword.Text = String.Empty;

        //    string email = txtEmail.Text.Trim();
        //    string name = txtName.Text.Trim();
        //    string password = txtPassword.Text.Trim();
        //    if (!email.Equals("") && !name.Equals("") && !password.Equals(""))
        //    {
        //        string address = txtAddress.Text.Trim();
        //        int departmentID = Int32.Parse(ddlDepartment.SelectedValue);
        //        int permissionID = Int32.Parse(ddlPermission.SelectedValue);
                
        //        EnStaff staff = new EnStaff();
        //        staff.staffName = name;
        //        staff.staffEmail = email;
        //        staff.staffAddress = address;
        //        staff.staffPassword = Utilities.UtiGeneralClass.Md5Encode(password);
        //        staff.staffPermissionID = permissionID;
        //        staff.staffCompanyID = 1;
        //        staff.staffDepartmentID = departmentID;

        //        BuStaff.insStaffOpt(staff);
        //        FillGrid();
        //        ClearControl();
        //    }

        //    if (email.Equals(""))
        //    {
        //        lblErrorEmail.Text = "không đuợc để trống";
        //    }
            
        //    if (name.Equals(""))
        //    {
        //        lblErrorName.Text = "không đuợc để trống";
        //    }
                
        //    if(password.Equals(""))
        //    {
        //        lblErrorPassword.Text="không đuợc để trống";
        //    }   
        //}

        //public void ClearControl()
        //{
        //    txtEmail.Text = String.Empty;
        //    txtAddress.Text = String.Empty;
        //    txtName.Text = String.Empty;
        //    txtPassword.Text = String.Empty;
        //    ddlDepartment.SelectedValue = "1";
        //    ddlPermission.SelectedValue = "1";
        //}

        protected void GVListStaff_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            EnStaff staff = new EnStaff();
            staff.staffEmail = GVListStaff.DataKeys[e.RowIndex].Values[0].ToString();
            BuStaff.delStaff(staff, Session["companyid"].ToString());
            FillGrid();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateNewStaff.aspx");
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            Response.Redirect("ImportUser.aspx");
        }

        protected void GVListStaff_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string currentCommand = e.CommandName;
            if (currentCommand.Equals("ResetPass"))
            {
                EnStaff staff = new EnStaff();
                staff.staffEmail = e.CommandArgument.ToString();
                staff.staffPassword = Utilities.UtiGeneralClass.Md5Encode("123456");
                BuStaff.resetPass(staff, Session["companyid"].ToString());
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Khôi phục mật khẩu thành công!');", true);  
            }
        }
    }
}